
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Mistral.Interface.Features;
using Mistral.Options;
using Mistral.Options.Impl;

namespace Mistral.Plugin
{
	public partial class OptionsForm : Form
	{
		public OptionsForm()
		{
			InitializeComponent();
			new Feature_TreeView_PropertyGridBinder( _treeView, _propertyGrid ).Activate();
			RefreshTree();
		}
		
		public void RefreshTree()
		{
			_treeView.Nodes.Clear();

			foreach ( OptionSet optionSet in App.Instance.OptionManager.OptionSets )
			{
				IOptionCategory root = optionSet.Root;
				if( root != null && root.Categories.Length > 0 )
				{
					TreeNode node = new TreeNode( Path.GetFileNameWithoutExtension(optionSet.FileName) );
					_treeView.Nodes.Add( node );
					new Feature_TreeView_Builder( node, (ICollection)root.Categories ).Activate();
				}
			}

		}

		private void saveToolStripMenuItem_Click( object sender, System.EventArgs e )
		{
			App.Instance.OptionManager.Save( true );
		}
		
	}
	
	class OptionTree
	{
		OptionCategoryMerged m_root = new OptionCategoryMerged( "root" );
		
		public void Fill()
		{
			foreach( OptionSet optionSet in App.Instance.OptionManager.OptionSets )
			{
				foreach ( IOptionCategory category in optionSet.Root.Categories )
				{
					m_root.Fill( category );
				}
			}
		}
		
		public void FillTree( TreeNode node )
		{
			
		}
		
	}
	
	class OptionCategoryMerged
	{
		List<OptionCategoryMerged> m_categories = new List<OptionCategoryMerged>();
		List<IOption> m_options = new List<IOption>( );
		string m_name;

		public string Name
		{
			get { return m_name; }
		}

		public OptionCategoryMerged( string name )
		{
			m_name = name;
		}

		public void AddOption( IOption option )
		{
			foreach ( IOption option1 in m_options )
			{
				if( string.Compare(option1.Name, option.Name, true)==0)
				{
					return;
				}
			}
			m_options.Add( option );
		}
		
		private OptionCategoryMerged GetOrCreate( IOptionCategory category )
		{
			OptionCategoryMerged result = null;
			foreach( OptionCategoryMerged categoryMerged in m_categories )
			{
				if( string.Compare( categoryMerged.Name, category.Name, true ) == 0 )
				{
					result = categoryMerged;
					break;
				}
			}
			if( result == null )
			{
				result = new OptionCategoryMerged( category.Name );
				m_categories.Add( result );
			}
			return result;
		}

		public void Fill( IOptionCategory category )
		{
			OptionCategoryMerged categoryMerged;
			foreach ( IOptionCategory optionCategory in category.Categories )
			{
				categoryMerged = GetOrCreate( category );	
				categoryMerged.Fill( optionCategory );
			}
			
			categoryMerged = GetOrCreate( category );				
			foreach( IOption option in category.Options )
			{
				categoryMerged.AddOption( option );
			}
			
		
		}

		public void FillTree( TreeNode node )
		{

		}
		
	}	
	
}